// pages/get-cash/get-cash.js
const requestToPutAccountBalance = require('../../utils/request.js').requestToPutAccountBalance

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    balance:0,
    amount:"",
    errortip:false,
    btn_disable:true,

    showResult: false,
    result: { isSuccess: true, text: "提现申请成功", tip: "你的提现已申请成功，24小时内资金会打到你的微信零钱，请耐心等待。", btnText1: '返回首页' }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = JSON.parse(options.data)
    this.setData({
      balance: data.balance
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  bindinput:function(e){
    // console.log(e.detail.value)
    let errortip = false
    let btn_disable = false

    if (e.detail.value > 20000 || e.detail.value > this.data.balance / 100) {
      errortip = true
      btn_disable = true
    }
    
    let oldInput = this.data.amount
    let newInput = e.detail.value

    if (newInput.indexOf('.') !== newInput.lastIndexOf('.')) {
      this.setData({
        amount: oldInput
      })
    } else {

      if (newInput.indexOf('.') >= 0 && newInput.split('.')[1].length > 2) {
        this.setData({
          amount: oldInput
        })
      } else {
        this.setData({
          amount: newInput
        })
      }
    }

    
    this.setData({
      
      errortip: errortip,
      btn_disable: btn_disable
    })

    
  },
  bindtap: function () {
    let amount = parseInt(this.data.amount)*100
    if (!amount){
      wx.showToast({
        title: '提现金额需要大于1元',
        icon: 'none',
        mask: true,
      })
      return
    }

    amount = parseInt(this.data.amount * 100)
    requestToPutAccountBalance(amount, res => {

      console.log('提现成功')
      console.log(res)
      app.updateAccountPageData()

      this.setData({
        showResult: true
      })
      wx.setNavigationBarTitle({
        title: "提现申请成功"
      })
    })

  },

  bindresultbtn1tap: function (e) {
    wx.navigateBack({
      delta: 1
    })
  },

  bindallmoneytap:function(){
    let amount = 2000000
    if (this.data.balance < amount){
      amount = this.data.balance
    }
    this.setData({
      amount: amount/100,
      btn_disable: false,
    })
  }
})